<template>
  <div>
    <el-form :model="form" label-width="120" label-position="right">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" style="margin-left: 15px">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item align="center" style="margin-right: 55%">
        <el-button @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
    }
  },
  methods: {
    login() {
      this.$http.post('/permission/getMenu', this.form).then((res) => {
        res = res.data
        console.log(res)
        if (res.code === 20000) {
          // 避免重复登录
          this.$store.commit('clearMenu')
          this.$store.commit('setMenu', res.data.menu)
          this.$store.commit('setToken', res.data.token)
          this.$store.commit('addMenu', this.$router)
          this.$router.push({ name: 'home' })
        } else {
          this.$message.warning(res.data.message)
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.el-form {
  margin-left: 30%;
  margin-top: 10%;
}
.el-input {
  width: 500px;
}
</style>
